package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleStaticHtmlService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.mess.CreateArticleMess;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-10-24 11:26
 */
@Service
@Slf4j
public class ApArticleStaticHtmlServiceImpl implements ApArticleStaticHtmlService {

    @Autowired
    private Configuration configuration;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    @Async
    public void createStaticHtml(ApArticle apArticle, String content) throws IOException, TemplateException {

        //freemarker的模板对象，获取模板
        Template template = configuration.getTemplate("article.ftl");
        Map params = new HashMap();


        List<Map> maps = JSON.parseArray(content,Map.class); // -> List<Map>
        params.put("content",maps);

        StringWriter stringWriter = new StringWriter(); //输出字符流
        template.process(params, stringWriter); //freemarker输出内存
        //输入字节流
        ByteArrayInputStream inputStream = new ByteArrayInputStream(stringWriter.toString()
                .getBytes(StandardCharsets.UTF_8));
        String path = fileStorageService.
                uploadHtmlFile("", apArticle.getId() + ".html", inputStream);//minIO读取内存

        //更新主表static_url字段方便后续进行列表跳转
        apArticleMapper.update(null,Wrappers.<ApArticle>lambdaUpdate()
                .set(ApArticle::getStaticUrl,path).eq(ApArticle::getId,apArticle.getId())
        );

        //发送消息
        CreateArticleMess createArticleMess = new CreateArticleMess();
        BeanUtils.copyProperties(apArticle,createArticleMess);
        createArticleMess.setStaticUrl(path);
        createArticleMess.setContent(content);

        kafkaTemplate.send(WmNewsMessageConstants.ARTICLE_CREATE_TOPIC
                ,JSON.toJSONString(createArticleMess));
    }

}
